function insertSutrasCategorySelect(sutraID, sutrasCategory){
    sclist = $('#sclist').html();
    var doTtmpl = doT.template(sclist);

    var offset = 0;
    data = {
        'offset': offset,
        'limit': 0,
        'getall': 1,
    }
    var api = '/backstage/sutra/category/list';
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: 'GET',
        data: data,
        async: false,
        dataType: 'json',
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status==200){
                scid = '#' + sutrasCategory
                $(scid).html(doTtmpl(data));
                sscid = '#' + 'sc_' + sutraID;
                $(sscid).attr('selected', 'selected')
            }
            else{
                alert('请求异常, 请刷新')
            }
        },
    });
}


function editionCreate(){
    $('#sutras_category_demo').css('display','none')
    $('#main').html(getEditionCreateDemo());
    $('#sutras_category').remove();
    insertSutrasCategorySelect(false, 'sutras_category');

    var sutra_id;
    $('#sutras_category').change(function(){
        sutra_id = $(this).val();
    })
    $('#create_edition').on('click', function(){
        edition_name = $('#edition_name').val();
        edition_page_num = $('#edition_page_num').val();
        edition_price = $('#edition_price').val();
        edition_img = $('#edition_img').val();
        char_txt = $('#char_txt').val();
        edition_desc = $('#edition_desc').val();
        judge = !edition_name || !sutra_id || !edition_page_num || !edition_desc || !edition_img
        if (judge) {
            return
        };
        var data = new FormData();
        data.append('sutra_id', sutra_id);
        data.append('name', edition_name);
        data.append('desc', edition_desc);
        data.append('price', edition_price);
        data.append('total_page_num', edition_page_num);
        data.append('char_txt', $('#char_txt')[0].files[0])
        data.append('cover_img', $('#edition_img')[0].files[0])
        var csrftoken = getCookie('csrftoken');
        $.ajax({
            url: '/backstage/sutra/edition',
            type: 'POST',
            data: data,
            async: false,
            processData: false,
            contentType: false,
            cache: false,
            beforeSend: function(xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            },
            success: function(data, status){
                if(data.status==200){
                    alert('创建成功');
                    history.back();
                }
                else{
                    alert('请求异常, 请刷新')
                }
            },
        });
    });
}


function editionUpdate(Id){
    res = editionGet(Id);
    var sutra_id = res.sutra_id;
    insertSutrasCategorySelect(res.sutra_id, 'sutras_category');
    console.log(res)
    $('#edition_name').val(res.name);
    $('#edition_page_num').val(res.total_page_num);
    $('#edition_price').val(res.price);
    $('#edition_img_url').attr('href', res.cover_url);
    $('#edition_img_url').attr('target', '_blank');
    $('#edition_img_url').html(' (原封面链接)');
    $('#char_url').attr('href', res.char_url);
    $('#char_url').attr('target', '_blank');
    $('#char_url').html(' (原文字链接)');
    $('#edition_desc').val(res.desc);
    // sc_{{=it.data[i].id}}

    $('#sutras_category').change(function(){
        sutra_id = $(this).val();
    })

    $('#update_edition').on('click', function(){
        var data = new FormData();
        data.append('sutra_id', sutra_id);
        data.append('name', $('#edition_name').val());
        data.append('desc', $('#edition_desc').val());
        data.append('price',  $('#edition_price').val());
        data.append('total_page_num', $('#edition_page_num').val());
        if ($('#char_txt')[0].files[0]) {
            data.append('char_txt', $('#char_txt')[0].files[0])
        }
        if ($('#edition_img')[0].files[0]) {
            data.append('cover_img', $('#edition_img')[0].files[0])
        }
        var csrftoken = getCookie('csrftoken');
        $.ajax({
            url: '/backstage/sutra/edition/' + Id,
            type: 'PUT',
            data: data,
            async: false,
            processData: false,
            contentType: false,
            cache: false,
            beforeSend: function(xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            },
            success: function(data, status){
                if(data.status==200){
                    alert('修改成功');
                    url = 'edition.html?operation=list&sutra_id='
                    url = url + sutra_id
                    window.location.href=url;
                }
                else{
                    alert('请求异常, 请重试')
                }
            },
        });
    });
}


function editionGet (Id) {
    var category = false;
    var api = '/backstage/sutra/edition/' + Id + '/details';
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: 'GET',
        async: false,
        dataType: 'json',
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status==200){
                category = data.data
            }
            else{
                alert('请求异常, 请重试')
            }
        },
    });
    return category
}
editionID = getQueryString('edition_id');
editionUpdate(editionID)